<?php

$filename = $argv[1];
$outputpath = $argv[2];

print $filename;

ini_set("max_execution_time", "30000");
    
    /**
     * GENERAL FUNCTIONS
     */
    function findValues($byte1, $byte2){
      $byte1 = hexdec(bin2hex($byte1));                        
      $byte2 = hexdec(bin2hex($byte2));                        
      return ($byte1 + ($byte2*256));
    }

/**
     * Great function slightly modified as posted by Minux at
     * http://forums.clantemplates.com/showthread.php?t=133805
     */
    function html2rgb($input) {
      $input=($input[0]=="#")?substr($input, 1,6):substr($input, 0,6);
      return array(
       hexdec( substr($input, 0, 2) ),
       hexdec( substr($input, 2, 2) ),
       hexdec( substr($input, 4, 2) )
      );
    }   


if (!file_exists($filename)) 
{
        print "Error: WAV file not generated. Please verify directory write and execute permissions.";
        exit;
}
      
      /**
       * Below as posted by "zvoneM" on
       * http://forums.devshed.com/php-development-5/reading-16-bit-wav-file-318740.html
       * as findValues() defined above
       * Translated from Croation to English - July 11, 2011
       */
      $handle = fopen ($filename, "r");
      //dohvacanje zaglavlja wav datoteke
      $heading[] = fread ($handle, 4);
      $heading[] = bin2hex(fread ($handle, 4));
      $heading[] = fread ($handle, 4);
      $heading[] = fread ($handle, 4);
      $heading[] = bin2hex(fread ($handle, 4));
      $heading[] = bin2hex(fread ($handle, 2));
      $heading[] = bin2hex(fread ($handle, 2));
      $heading[] = bin2hex(fread ($handle, 4));
      $heading[] = bin2hex(fread ($handle, 4));
      $heading[] = bin2hex(fread ($handle, 2));
      $heading[] = bin2hex(fread ($handle, 2));
      $heading[] = fread ($handle, 4);
      $heading[] = bin2hex(fread ($handle, 4));
      
      //bitrate wav datoteke
      $peek = hexdec(substr($heading[10], 0, 2));
      $byte = $peek / 8;
      
      //provjera da li se radi o mono ili stereo wavu
      $channel = hexdec(substr($heading[6], 0, 2));
      
      if($channel == 2){
        $omjer = 40;
      }
      else{
        $omjer = 80;
      }
      
      while(!feof($handle)){
        $bytes = array();
        //get number of bytes depending on bitrate
        for ($i = 0; $i < $byte; $i++){
          $bytes[$i] = fgetc($handle);
        }
        switch($byte){
          //get value for 8-bit wav
          case 1:
              $data[] = findValues($bytes[0], $bytes[1]);
              break;
          //get value for 16-bit wav
          case 2:
            if(ord($bytes[1]) & 128){
              $temp = 0;
            }
            else{
              $temp = 128;
            }
            $temp = chr((ord($bytes[1]) & 127) + $temp);
            $data[]= floor(findValues($bytes[0], $temp) / 256);
            break;
        }
        //skip bytes for memory optimization
        fread ($handle, $omjer);
      }
      
      // close and cleanup
      fclose ($handle);
      unlink($filename);
      
      /**
       * Image generation
       */
    
      // how much detail we want. Larger number means less detail
      // (basically, how many bytes/frames to skip processing)
      // the lower the number means longer processing time
      define("DETAIL", 5);
      
      // get user vars from form
      $width =  500;
      $height =  100;
      $foreground =  "#FF0000";
      $background =  "#FFFFFF";
      
      // create original image width based on amount of detail
      $img = imagecreatetruecolor(sizeof($data) / DETAIL, $height);
      
      // fill background of image
      if ($background == "") {
        imagesavealpha($img, true);
        $transparentColor = imagecolorallocatealpha($img, 0, 0, 0, 127);
        imagefill($img, 0, 0, $transparentColor);
      } else {
        list($r, $g, $b) = html2rgb($background);
        imagefilledrectangle($img, 0, 0, sizeof($data) / DETAIL, $height, imagecolorallocate($img, $r, $g, $b));
      }

      // generate foreground color
      list($r, $g, $b) = html2rgb($foreground);
      
      // loop through frames/bytes of wav data as genearted above
      for($d = 0; $d < sizeof($data); $d += DETAIL) {
        // relative value based on height of image being generated
        // data values can range between 0 and 255
        $v = (int) ($data[$d] / 255 * $height);
        // draw the line on the image using the $v value and centering it vertically on the canvas
        imageline($img, $d / DETAIL, 0 + ($height - $v), $d / DETAIL, $height - ($height - $v), imagecolorallocate($img, $r, $g, $b));
      }
      
      //header("Content-Type: image/png");
      
      // want it resized?
      if ($width) {
      
        // resample the image to the proportions defined in the form
        $rimg = imagecreatetruecolor($width, $height);
        // save alpha from original image
        imagesavealpha($rimg, true);
        imagealphablending($rimg, false);
        // copy to resized
        imagecopyresampled($rimg, $img, 0, 0, 0, 0, $width, $height, sizeof($data) / DETAIL, $height);
        imagepng($rimg,$outputpath);
        imagedestroy($rimg);
      
      } else {
      
        // print out at it's raw width (size of $data / detail level)
        imagepng($img,$outputpath);
        imagedestroy($img);
      
      }

?>

